package com.yxhl.zoume.core.func.pay.presenter;

import android.content.Context;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yxhl.zoume.BuildConfig;
import com.yxhl.zoume.ZMConfig;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.pay.view.WeChatPayView;
import com.yxhl.zoume.data.http.rest.param.pay.WXPayOrderParam;
import com.yxhl.zoume.data.http.rest.response.pay.WXPayOrderResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.pay.WeChatPayPreOrderUseCase;
import com.yxhl.zoume.utils.LOG;

import javax.inject.Inject;

/**
 * Created by vhawk on 16/3/31.
 */
public class WeChatPayPresenter extends BasePresenter implements IPresenter {
    private WeChatPayPreOrderUseCase useCase;
    private WeChatPayView payView;
    private IWXAPI msgapi;

    @Inject
    public WeChatPayPresenter(Context context, WeChatPayPreOrderUseCase useCase) {
        super(context);
        this.useCase = useCase;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        useCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        this.payView = (WeChatPayView) view;
    }

    /**
     * 生成微信预订单
     *
     * @param context
     * @param ticketOrderId
     */
    public void getWeChatPayOrderInfo(final Context context, String ticketOrderId, String couponId, String discountFee) {
        msgapi = WXAPIFactory.createWXAPI(context, ZMConfig.WX_APPID, false);
        msgapi.registerApp(ZMConfig.WX_APPID);

        if (msgapi.isWXAppInstalled()) {
            WXPayOrderParam param = new WXPayOrderParam(getMobile(), getToken());
            param.setCouponUserRelId(couponId)
                    .setDisCountFee(discountFee)
                    .setId(ticketOrderId);
            useCase.execute(param, new ZMSubscriber<WXPayOrderResponse>() {
                @Override
                public void onNext(WXPayOrderResponse wxPayOrderResponse) {
                    super.onNext(wxPayOrderResponse);
                    if (wxPayOrderResponse.isSucc()) {
                        // 打开微信客户端进行支付
                        openWXPay(context, wxPayOrderResponse);
                        // 取消支付等待
                        payView.dismissWaitingDialog();
                    } else {
                        payView.showErrorMessage(wxPayOrderResponse.getResultMessage());
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }
            });
        } else {
            payView.showWechatUninstalledDialog();
        }
    }

    /**
     * 生成微信预订单
     *
     * @param context
     * @param ticketOrderId
     */
    public void getWeChatPayOrderInfo(final Context context, String ticketOrderId) {
        getWeChatPayOrderInfo(context, ticketOrderId, null, null);
    }


    /**
     * 生成微信充值
     *
     * @param context
     * @param totalFee
     */
    public void getWeChatRechargeSignedStr(final Context context, String totalFee) {
        msgapi = WXAPIFactory.createWXAPI(context, ZMConfig.WX_APPID, false);
        msgapi.registerApp(ZMConfig.WX_APPID);

        if (msgapi.isWXAppInstalled()) {
            WXPayOrderParam param = new WXPayOrderParam(getMobile(), getToken());
            param.setBizType(6)
                    .setClientVersion(BuildConfig.VERSION_NAME)
                    .setTotalFee(totalFee);
            useCase.execute(param, new ZMSubscriber<WXPayOrderResponse>() {
                @Override
                public void onNext(WXPayOrderResponse wxPayOrderResponse) {
                    super.onNext(wxPayOrderResponse);
                    if (true == wxPayOrderResponse.isSucc()) {
                        // 打开微信客户端进行支付
                        openWXPay(context, wxPayOrderResponse);
                        // 取消支付等待
                        payView.dismissWaitingDialog();
                    } else {
                        payView.showErrorMessage(wxPayOrderResponse.getResultMessage());
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }
            });
        } else {
            payView.showWechatUninstalledDialog();
        }
    }

    /**
     * 调起微信支付
     *
     * @param context
     * @param payOrder
     */
    private void openWXPay(Context context, WXPayOrderResponse payOrder) {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(context, ZMConfig.WX_APPID);
        PayReq req = new PayReq();
        req.appId = payOrder.getAppid();
        req.partnerId = payOrder.getPartnerid();
        req.prepayId = payOrder.getPrepayid();
        req.nonceStr = payOrder.getNoncestr();
        req.timeStamp = payOrder.getTimestamp();
        req.packageValue = ZMConfig.WX_PACKAGE;
        req.sign = payOrder.getSign();
        boolean isSuccess = iwxapi.sendReq(req);
        LOG.v("vhawk", "isSuccess =" + isSuccess);
    }
}
